home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINICONS / ANIMATOR.ZIP / MAIN.C < prev    next >
C/C++ Source or Header  |  1993-04-16  |  16KB  |  606 lines

  1. #include "animator.h"
  2. #include <string.h>
  3.  
  4. VOID NEAR PASCAL ParseCmdLine (LPSTR);
  5.  
  6. //////////////////////////////////////////////////////////////////////////
  7. //  WinMain - Obviously sets up the Window class, loads string
  8. //            and menu resources, creates the frame window,
  9. //            sets the timer function pointer, toolhelp notify,
  10. //            and allows the non-preemted loop to start.
  11. //////////////////////////////////////////////////////////////////////////
  12.  
  13. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
  14.     LPSTR lpszCmdLine, int nCmdShow)
  15. {
  16.     HACCEL      hAccel;
  17.     WNDCLASS    wndclass;
  18.     MSG         msg;
  19.  
  20.     if ((short)GetVersion() < 0x0310)
  21.     {
  22.         MESSAGE (IDS_WinVer);
  23.         return 0;
  24.     }
  25.  
  26.     _hInst = hInst;
  27.  
  28.     // Load all application strings... (defined in .RC file)
  29.     LoadStr (IDS_AppName, _szAppName);
  30.     LoadStr (IDS_AppTitle, _szTitleBar);
  31.     LoadStr (IDS_ChildClassName, _szChildClass);
  32.     LoadStr (IDS_StatBarClassName, _szStatBarClass);
  33.     LoadStr (IDS_Extension, _szExtension);
  34.     LoadStr (IDS_Untitled, _szUntitled);
  35.     LoadStr (IDS_TimerIntervalKey, _szTimeInt);
  36.     LoadStr (IDS_InfoSection, _szInfo);
  37.     LoadStr (IDS_NumIconsKey, _szNumIcons);
  38.     LoadStr (IDS_LinkFileKey, _szLinkFile);
  39.     LoadStr (IDS_IconsSection, _szIconSection); 
  40.     LoadStr (IDS_IconFrameKey, _szIcon);
  41.     LoadStr (IDS_AutoAnimateKey, _szAutoAnimateKey);
  42.  
  43.     if (hPrevInst)
  44.     {
  45.         HWND hwnd = FindWindow (_szAppName,NULL);
  46.         BringWindowToTop (hwnd);
  47.         return 0;
  48.     }
  49.  
  50.     _hmenuMain        = LoadMenu(_hInst, MAINMENU);
  51.     _hmenuChild       = LoadMenu(_hInst, CHILDMENU);
  52.     _hmenuMainWindow  = 
  53.     _hmenuChildWindow = GetSubMenu(_hmenuChild, WNDMENUPOS);
  54.  
  55.     _hpnBlack = CreatePen (PS_SOLID, 1, GetSysColor(COLOR_BTNTEXT));
  56.     _hpnGray  = CreatePen (PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
  57.     _hpnWhite = CreatePen (PS_SOLID, 1, GetSysColor(COLOR_BTNHIGHLIGHT));
  58.  
  59.     if (!hPrevInst) 
  60.     {
  61.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  62.         wndclass.lpfnWndProc    = FrameProc;
  63.         wndclass.cbClsExtra     = 0;
  64.         wndclass.cbWndExtra     = 0;
  65.         wndclass.hInstance      = _hInst;
  66.         wndclass.hIcon          = LoadIcon(_hInst, MAINICON);
  67.         wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  68.         wndclass.hbrBackground  = COLOR_APPWORKSPACE + 1;
  69.         wndclass.lpszMenuName   = NULL;
  70.         wndclass.lpszClassName  = _szAppName;
  71.  
  72.         if (!RegisterClass(&wndclass)) return 0;
  73.  
  74.         wndclass.lpfnWndProc    = ChildProc;
  75.         wndclass.cbWndExtra     = WW_ENDOFWORDS;
  76.         wndclass.hIcon          = LoadIcon(_hInst, CHILDICON);
  77.         wndclass.hbrBackground  = COLOR_WINDOW + 1;
  78.         wndclass.lpszClassName  = _szChildClass;
  79.         
  80.         if (!RegisterClass(&wndclass)) return 0;
  81.  
  82.         wndclass.lpfnWndProc    = StatusBarProc;
  83.         wndclass.cbWndExtra     = 0;
  84.         wndclass.hIcon          = NULL;
  85.         wndclass.hbrBackground  = COLOR_BTNFACE + 1;
  86.         wndclass.lpszClassName  = _szStatBarClass;
  87.  
  88.         if (!RegisterClass (&wndclass)) return 0;
  89.     }
  90.  
  91.     _lpfnTimer = MakeProcInstance ((FARPROC)TimerCallback, _hInst);
  92.     if (_lpfnTimer == NULL) goto MErr0;
  93.  
  94.     _lpfnNotify = MakeProcInstance ((FARPROC)NotifyProc, _hInst);
  95.     if (_lpfnNotify == NULL) goto MErr1;
  96.  
  97.     _lpfnSettings = MakeProcInstance ((FARPROC)SettingsDlg, _hInst);
  98.     if (_lpfnSettings == NULL) goto MErr2;
  99.  
  100.     _lpfnAbout = MakeProcInstance ((FARPROC)About, _hInst);
  101.     if (_lpfnAbout == NULL) goto MErr3;
  102.  
  103.     _lpfnBroadcast = MakeProcInstance ((FARPROC)BroadcastProc, _hInst);
  104.     if (_lpfnBroadcast == NULL) goto MErr4;
  105.  
  106.     _hwndFrame = CreateWindow(_szAppName, _szTitleBar,
  107.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  108.         0, 0, FRAME_X, FRAME_Y,
  109.         NULL, _hmenuMain, _hInst, NULL);
  110.  
  111.     if (!IsWindow(_hwndFrame)) goto MErr5;
  112.  
  113.     if (!RestorePosition (_hwndFrame, nCmdShow)) 
  114.     {
  115.         CenterWindow (_hwndFrame);
  116.     }
  117.     
  118.     if (lpszCmdLine)
  119.     {
  120.         ParseCmdLine (lpszCmdLine);
  121.         RefreshAnimations ();
  122.     }
  123.  
  124.     ShowWindow(_hwndFrame, SW_SHOW);
  125.     UpdateWindow(_hwndFrame);
  126.     
  127.     hAccel = LoadAccelerators(_hInst, ACCELTABLE);
  128.     if (hAccel == NULL) goto MErr5;
  129.  
  130.     while (GetMessage(&msg, NULL, 0, 0))
  131.     {
  132.         if (!TranslateMDISysAccel(_hwndClient, &msg) &&
  133.             !TranslateAccelerator(_hwndFrame, hAccel, &msg)) 
  134.         {
  135.             TranslateMessage(&msg);
  136.             DispatchMessage(&msg);
  137.         }
  138.     }
  139.  
  140. MErr5:
  141.     FreeProcInstance (_lpfnBroadcast);
  142. MErr4:
  143.     FreeProcInstance (_lpfnAbout);
  144. MErr3:
  145.     FreeProcInstance (_lpfnSettings);
  146. MErr2:
  147.     FreeProcInstance (_lpfnNotify);
  148. MErr1:
  149.     FreeProcInstance (_lpfnTimer);    
  150. MErr0:
  151.     DeletePen (_hpnBlack);
  152.     DeletePen (_hpnWhite);
  153.     DeletePen (_hpnGray);
  154.  
  155.     return msg.wParam;
  156. }
  157.  
  158.  
  159.  
  160. //////////////////////////////////////////////////////////////////////////
  161. // CenterWindow - takes any window (more especially dialog boxes) and
  162. //                centers it on the display.
  163. //////////////////////////////////////////////////////////////////////////
  164.  
  165. VOID WINAPI CenterWindow (HWND hDlg)
  166. {
  167.   RECT rc;
  168.  
  169.   GetWindowRect(hDlg,&rc);
  170.   SetWindowPos(hDlg,NULL,
  171.    (GetSystemMetrics(SM_CXSCREEN) - (rc.right - rc.left)) / 2,
  172.    (GetSystemMetrics(SM_CYSCREEN) - (rc.bottom - rc.top)) / 3,
  173.     0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
  174. }
  175.  
  176.  
  177.  
  178. //////////////////////////////////////////////////////////////////////////
  179. // RestorePosition() - Gets the formated information for the 
  180. // WINDOWPLACEMENT structure from the WIN.INI file, parses it by
  181. // calling IntFromString(), and then calls SetWindowPlacement().
  182. //////////////////////////////////////////////////////////////////////////
  183.  
  184. BOOL WINAPI RestorePosition (HWND hwnd, short nCmdShow)
  185. {
  186.     char szFmt[80];
  187.     LPSTR lpszCur = (LPSTR)szFmt;
  188.     WINDOWPLACEMENT wpl;
  189.  
  190.     GetProfileString ((LPSTR)_szTitleBar, (LPSTR)"Position\0", (LPSTR)"\0",
  191.         (LPSTR)szFmt, sizeof(szFmt));
  192.  
  193.     if (szFmt[0] == '\0' || IntFromString((LPSTR FAR *)&lpszCur) != 9)
  194.     {
  195.         return FALSE;
  196.     }
  197.     else
  198.     {
  199.         // Format in the WIN.INI file for the position restoration is:
  200.         // [Animator]
  201.         // Position = 9 showcmd minX minY maxX maxY normX normY normDX normDY
  202.         //
  203.         wpl.length = sizeof(wpl); 
  204.         wpl.showCmd = (UINT)IntFromString((LPSTR FAR *)&lpszCur);
  205.         wpl.showCmd = (UINT)nCmdShow;
  206.         wpl.ptMinPosition.x = IntFromString((LPSTR FAR *)&lpszCur);
  207.         wpl.ptMinPosition.y = IntFromString((LPSTR FAR *)&lpszCur);
  208.         wpl.ptMaxPosition.x = IntFromString((LPSTR FAR *)&lpszCur);
  209.         wpl.ptMaxPosition.y = IntFromString((LPSTR FAR *)&lpszCur);
  210.         wpl.rcNormalPosition.left = IntFromString((LPSTR FAR *)&lpszCur);
  211.         wpl.rcNormalPosition.top = IntFromString((LPSTR FAR *)&lpszCur);
  212.         wpl.rcNormalPosition.right = IntFromString((LPSTR FAR *)&lpszCur);
  213.         wpl.rcNormalPosition.bottom = IntFromString((LPSTR FAR *)&lpszCur);
  214.  
  215.         return SetWindowPlacement(hwnd, &wpl);
  216.     }
  217. }
  218.  
  219.  
  220.  
  221. //////////////////////////////////////////////////////////////////////////
  222. // RecordPosition() - Gets the window placement and show state, by
  223. // using the GetWindowPlacement function.  the length member MUST
  224. // be filled or it will fail.  We then write the information to 
  225. // the WIN.INI with the info separated by spaces.
  226. //////////////////////////////////////////////////////////////////////////
  227.  
  228. VOID WINAPI RecordPosition(HWND hwnd)
  229. {
  230.     WINDOWPLACEMENT wpl;
  231.  
  232.     char szFmt[80];
  233.  
  234.     wpl.length = sizeof(wpl);    // necessary...
  235.     GetWindowPlacement(hwnd, &wpl);
  236.  
  237.     wsprintf((LPSTR)szFmt, "%d %d %d %d %d %d %d %d %d %d",
  238.         9,
  239.         wpl.showCmd,
  240.         wpl.ptMinPosition.x,
  241.         wpl.ptMinPosition.y,
  242.         wpl.ptMaxPosition.x,
  243.         wpl.ptMaxPosition.y,
  244.         wpl.rcNormalPosition.left,
  245.         wpl.rcNormalPosition.top,
  246.         wpl.rcNormalPosition.right,
  247.         wpl.rcNormalPosition.bottom
  248.     );
  249.  
  250.     WriteProfileString((LPSTR)_szTitleBar, (LPSTR)"Position", (LPSTR)szFmt);
  251. }
  252.  
  253.  
  254.  
  255. //////////////////////////////////////////////////////////////////////////
  256. // IntFromString() - Our version of the C function itoa, with a little
  257. // more flexibility given our situation: a string of ints separated by
  258. // spaces.
  259. //////////////////////////////////////////////////////////////////////////
  260.  
  261. short WINAPI IntFromString(LPSTR FAR * lplpsz)
  262. {
  263.     LPSTR lpsz = *lplpsz;
  264.     short i = 0;
  265.     char ch;
  266.     BOOL fNeg;
  267.  
  268.     while (*lpsz == ' ')
  269.         lpsz++;
  270.  
  271.     fNeg = FALSE;
  272.     while (ch = *lpsz++)
  273.     {
  274.         if (ch == '-')
  275.         {
  276.             fNeg = !fNeg;
  277.             conti